package com.fearless.crm.workbench.web.controller;

import com.fearless.crm.settings.entity.User;
import com.fearless.crm.settings.service.UserService;
import com.fearless.crm.utils.DateTimeUtil;
import com.fearless.crm.utils.UUIDUtil;
import com.fearless.crm.vo.CustomerPaginationVo;
import com.fearless.crm.vo.CustomerRemarkVo;
import com.fearless.crm.workbench.entity.Contacts;
import com.fearless.crm.workbench.entity.Customer;
import com.fearless.crm.workbench.entity.CustomerRemark;
import com.fearless.crm.workbench.entity.Tran;
import com.fearless.crm.workbench.service.ContactsService;
import com.fearless.crm.workbench.service.CustomerRemarkService;
import com.fearless.crm.workbench.service.CustomerService;
import com.fearless.crm.workbench.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@RequestMapping("customer")
@Controller
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @Autowired
    private UserService userService;

    @Resource
    private ContactsService contactsService;

    @Resource
    private CustomerRemarkService customerRemarkService;


    @Autowired
    private TranService tranService;

//展现客户列表
    @RequestMapping("/getCustomerList.do")
    @ResponseBody
    public CustomerPaginationVo getCustomerListController(Customer customer, Integer pageNo, Integer pageSize){
        System.out.println("进入到getCustomerListController处理器方法");
        System.out.println("执行展现客户列表的操作");
        String name=customer.getName();
        String owner=customer.getOwner();
        String phone=customer.getPhone();
        String website=customer.getWebsite();

        //计算略过的记录条数
        int skipCount=(pageNo-1)*pageSize;
        Map map1=new Hashtable();
        map1.put("name",name);
        map1.put("owner",owner);
        map1.put("phone",phone);
        map1.put("website",website);
        map1.put("pageSize",pageSize);
        map1.put("skipCount",skipCount);


        CustomerPaginationVo customerPaginationVo=customerService.getCustomerListService(map1);

        return customerPaginationVo;
    }


//用于铺垫新增客户的模态窗口
    @RequestMapping(value = "/addCustomer.do",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> addCustomerController(){
        System.out.println("进入到addCustomerController处理器方法");

        System.out.println("铺垫新增客户的模态窗口");


        List<User> userList=userService.getUserListService();
        Map map=new HashMap();
        map.put("userList",userList);

        return map;
    }



//执行新增客户的操作
    @RequestMapping("/saveCustomer.do")
    @ResponseBody
    public Map<String,Object> saveCustomerController(HttpSession session,Customer customer){
        System.out.println("进入到saveCustomerController处理器法方法");
        System.out.println("执行客户的新增操作");
        String createTime= DateTimeUtil.getSysTime();
        String createBy=((User)session.getAttribute("user")).getName();
        String id= UUIDUtil.getUUID();

        customer.setCreateBy(createBy);
        customer.setId(id);
        customer.setCreateTime(createTime);


        boolean flag=customerService.saveCustomerService(customer);
        Map map=new HashMap();
        map.put("success",flag);

        return map;
    }


//铺垫修改客户的模态窗口
    @RequestMapping("/editCustomer.do")
    @ResponseBody
    public Map editCustomerController(String customerId){
        System.out.println("进入到editCustomerController处理器方法");
        System.out.println("执行铺垫修改客户的模态窗口");

        Customer customer=customerService.getCustomerByIdService(customerId);
        List<User> userList=userService.getUserListService();

        Map map=new HashMap();
        map.put("customer",customer);
        map.put("userList",userList);

        return map;

    }

//执行修改客户的操作
    @RequestMapping("/updateCustomer.do")
    @ResponseBody
    public Map updateCustomerController(HttpSession session,Customer customer){
        System.out.println("进入到updateCustomerController处理器方法");
        System.out.println("执行修改客户的操作");
        String editTime=DateTimeUtil.getSysTime();
        String editBy=((User)session.getAttribute("user")).getName();
        customer.setEditBy(editBy);
        customer.setEditTime(editTime);

        boolean flag=customerService.updateCustomer(customer);
        Map map=new HashMap();
        map.put("success",flag);

        return map;

    }


//执行删除客户的操作
    @RequestMapping("/deleteCustomer.do")
    @ResponseBody
    public Map deleteCustomerController(String[] id){
        System.out.println("进入到deleteCustomerController处理器方法中");
        System.out.println("执行客户的删除操作");


        boolean flag=customerService.deleteCustomer(id);
        Map map=new HashMap();
        map.put("success",flag);

        return map;

    }


//跳转到客户详细页
    @RequestMapping("/detail.do")
    public ModelAndView detailController(String id){
        System.out.println("进入到detailController处理器方法");
        System.out.println("跳转到客户详细页");

       Customer customer= customerService.getCustomerByIdDetail(id);
       ModelAndView mv=new ModelAndView();
       mv.addObject("customer",customer);
       mv.setViewName("forward:/workbench/customer/detail.jsp");

        return mv;
    }


//展现客户相关的备注
    @RequestMapping("/showCustomerRemark.do")
    @ResponseBody
    public Map showCustomerRemarkController(String id){
        System.out.println("进入到showCustomerRemarkController处理器方法");
        System.out.println("展现客户备注列表");

        List<CustomerRemarkVo> customerRemarkList=customerRemarkService.getRemarkListByCustomerId(id);




        Map map=new HashMap();

        map.put("customerRemark",customerRemarkList);


        return map;

    }

//展现联系人列表
    @RequestMapping("/showContactList.do")
    @ResponseBody
    public Map showContactListController(String id){
        System.out.println("进入到showContactListController处理器方法");
        System.out.println("执行展现联系人列表操作");


        List<Contacts> contactsList=contactsService.getContactListByCustomerId(id);

        Map map=new HashMap();
        map.put("contactList",contactsList);

        return map;

    }

//执行新增客户备注操作
    @RequestMapping("/addCustomerRemark.do")
    @ResponseBody
    public Map addCustomerRemarkController(HttpSession session,CustomerRemark customerRemark){
        System.out.println("进入到addCustomerRemarkController处理器方法");
        System.out.println("执行新增客户备注的操作");
        String id=UUIDUtil.getUUID();
        String createBy=((User)session.getAttribute("user")).getName();
        String createTime=DateTimeUtil.getSysTime();
        String editFlag="0";
        customerRemark.setEditFlag(editFlag);
        customerRemark.setCreateTime(createTime);
        customerRemark.setCreateBy(createBy);
        customerRemark.setId(id);

        Map map=customerRemarkService.addCustomerRemark(customerRemark);

        return map;
    }

//执行删除客户备注的操作
    @RequestMapping("/deleteCustomerRemark.do")
    @ResponseBody
    public Map deleteCustomerRemarkController(String id){
        System.out.println("进入到deleteCustomerRemarkController处理器方法");
        System.out.println("执行删除客户备注的操作");
        boolean flag=customerRemarkService.deleteCustomerRemark(id);
        Map map=new Hashtable();
        map.put("success",flag);

        return map;
    }

//铺垫修改客户备注信息的模态窗口
    @RequestMapping("/editCustomerRemark.do")
    @ResponseBody
    public Map editCustomerRemarkController(String id){
        System.out.println("进入到editCustomerRemarkController处理器方法");
        System.out.println("铺垫修改客户备注的模态窗口");
        CustomerRemark customerRemark=customerRemarkService.getCustomerRemark(id);

        Map map=new Hashtable();
        map.put("customerRemark",customerRemark);

        return map;

    }

//执行修改客户备注的操作
    @RequestMapping("/updateCustomerRemark.do")
    @ResponseBody
    public Map updateCustomerRemarkController(HttpSession session,CustomerRemark customerRemark){
        System.out.println("进入到updateCustomerRemarkController处理器方法");
        System.out.println("执行客户备注的修改操作");
        String editTime=DateTimeUtil.getSysTime();
        String editBy=((User)session.getAttribute("user")).getName();
        customerRemark.setEditFlag("1");
        customerRemark.setEditTime(editTime);
        customerRemark.setEditBy(editBy);

        boolean flag=customerRemarkService.updateCustomerRemark(customerRemark);

        Map map=new Hashtable();
        map.put("success",flag);
        return map;

    }

//执行删除联系人的操作
    @RequestMapping("/deleteContact.do")
    @ResponseBody
    public Map deleteContactController(String contactId){
        System.out.println("进入到deleteContactController处理器方法");
        System.out.println("执行联系人的删除操作");
        boolean flag=contactsService.deleteContactInCustomerDetail(contactId);
        Map map=new Hashtable();

        map.put("success",flag);

        return map;
    }
//铺垫新增联系人模态窗口
    @RequestMapping("/getUserList.do")
    @ResponseBody
    public Map getUserListController(){
        System.out.println("进入到getUserListController处理器方法");
        System.out.println("铺垫新增联系人模态窗口");
        List<User> userList=userService.getUserListService();

        Map map=new Hashtable();
        map.put("userList",userList);

        return map;
    }

//展现交易列表
    @RequestMapping("/showTransactionList.do")
    @ResponseBody
    public Map showTransactionController(HttpServletRequest request, String id){
        System.out.println("进入到showTransactionController处理器方法");
        System.out.println("执行交易列表的展现操作");
        Map<String,String> pMap= (Map<String, String>) request.getServletContext().getAttribute("pMap");//可能性对应map


        List<Tran> tranList=tranService.getTransactionList(id);

        List<Tran> newTranList=new ArrayList<>();

        for(Tran s:tranList){

          String stage=s.getStage();
          String possibility=pMap.get(stage);
            /*System.out.println(possibility);*/
          s.setPossibility(possibility);
          newTranList.add(s);

        }






        Map map=new Hashtable();
        map.put("transactionList",newTranList);

        return map;
    }

//在客户详细页新增联系人
    @RequestMapping("/addContact.do")
    @ResponseBody
    public Map addContactInCustomerDetailController(HttpSession session,Contacts contacts,String customerName){
        System.out.println("进入到addContactInCustomerDetailController处理器方法");
        System.out.println("执行在客户详细页新增联系人的操作");

       /* System.out.println("接收到的参数："+contacts.toString());
        System.out.println("收到的客户名称"+customerName);*/
       String id=UUIDUtil.getUUID();
       String createTime=DateTimeUtil.getSysTime();
       String createBy=((User)session.getAttribute("user")).getName();
       contacts.setId(id);
       contacts.setCreateTime(createTime);
       contacts.setCreateBy(createBy);

       boolean count=customerService.addContactInCustomerDetail(contacts,customerName);

        ConcurrentHashMap concurrentHashMap=new ConcurrentHashMap();

            concurrentHashMap.put("success",count);



        return concurrentHashMap;
    }

//在客户详细页修改客户
    @RequestMapping("/editCustomerInDetail.do")
    @ResponseBody
    public Map editCustomerInDetailController(String customerId){
        System.out.println("进入到editCustomerInDetailController处理器方法");
        System.out.println("执行铺垫在客户详细页铺垫修改客户的模态窗口");

        Map map=customerService.editCustomerInDetail(customerId);

        return map;
    }

//在客户详细页执行修改客户的操作
    @RequestMapping("/updateCustomerInDetail.do")
    @ResponseBody
    public Map updateCustomerInDetailController(Customer customer,HttpSession session){
        System.out.println("进入到updateCustomerInDetailController处理器方法");
        System.out.println("执行修改客户的操作");

/*
        System.out.println("接受的参数"+customer.toString());
*/
        String editBy=((User)session.getAttribute("user")).getName();
        String editTime=DateTimeUtil.getSysTime();

        customer.setEditBy(editBy);
        customer.setEditTime(editTime);

        Map map=customerService.updateCustomerInDetail(customer);

        return map;
    }

//在客户详细页执行删除客户的操作
    @PostMapping("/deleteCustomerInDetail.do")
    @ResponseBody
    public Map deleteCustomerInDetailController(@RequestParam("customerId") String id){
        System.out.println("进入到deleteCustomerInDetailController处理器方法");
        System.out.println("执行在客户详细页删除客户的操作");

        boolean flag=customerService.deleteCustomerInDetail(id);

        Map map=new HashMap();
        map.put("success",flag);

        return map;
    }

}
